package io.finer.erp.jeecg.bas.service.impl;

import java.util.List;
import java.util.Map;

import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.FillRuleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;

import io.finer.erp.jeecg.bas.entity.BasMaterial;
import io.finer.erp.jeecg.bas.entity.BasMaterialCategory;
import io.finer.erp.jeecg.bas.entity.SalesDelivery;
import io.finer.erp.jeecg.bas.entity.SalesDeliveryEntry;
import io.finer.erp.jeecg.bas.entity.ZrkOrderDeliveryEntryLk;
import io.finer.erp.jeecg.bas.entity.ZrkOrderProduct;
import io.finer.erp.jeecg.bas.mapper.SalesDeliveryEntryMapper;
import io.finer.erp.jeecg.bas.mapper.SalesDeliveryMapper;
import io.finer.erp.jeecg.bas.service.IBasMaterialCategoryService;
import io.finer.erp.jeecg.bas.service.IBasMaterialService;
import io.finer.erp.jeecg.bas.service.ISalesDeliveryService;
import io.finer.erp.jeecg.bas.service.IZrkOrderDeliveryEntryLkService;
import io.finer.erp.jeecg.bas.service.IZrkOrderProductService;
import io.finer.erp.jeecg.finance.mapper.FinaFreightMapper;
import io.finer.erp.jeecg.finance.service.IFinaReceiptService;
import io.finer.erp.jeecg.stock.entity.ZrkOrderProductMaterialLk;
import io.finer.erp.jeecg.stock.vo.FreightOrIoBillVo;

/**
 * @Description: 发货单表
 * @Author: libw
 * @Date:   2021-04-26
 * @Version: V1.0
 */
@Service
public class SalesDeliveryServiceImpl extends ServiceImpl<SalesDeliveryMapper, SalesDelivery> implements ISalesDeliveryService {

    @Autowired
    private SalesDeliveryMapper deliveryMapper;
    @Autowired
    private SalesDeliveryEntryMapper entryMapper;
    @Autowired
    private IFinaReceiptService finaReceiptService;
    @Autowired
    private IBasMaterialCategoryService categoryService;
    @Autowired
    private FinaFreightMapper finaFreightMapper;
    @Autowired
    private ISysBaseAPI baseApi;
    @Autowired
    private IZrkOrderDeliveryEntryLkService lkService;
    @Autowired
    private IZrkOrderProductService orderProductService;
    @Autowired
    private IBasMaterialService materialService;


    @Override
    @Transactional
    public void saveMach(SalesDelivery salesDelivery, List<SalesDeliveryEntry> list, Map<SalesDeliveryEntry, List<ZrkOrderProductMaterialLk>> maps){
        if(salesDelivery.getSalesType().equals(0)){
            String code= baseApi.getNoByCode("fh_no");
            salesDelivery.setCode(code);
        }
        deliveryMapper.insert(salesDelivery);
        if(list!=null && list.size()>0){
            for (SalesDeliveryEntry entry : list) {
                List<ZrkOrderProductMaterialLk> materialLkList = maps.get(entry);
                BasMaterialCategory category=categoryService.getById(entry.getCategoryId());
                if(category!=null){
                    entry.setCode(category.getCode());
                }
                entry.setDeliveryId(salesDelivery.getId());
                entryMapper.insert(entry);
                List<ZrkOrderDeliveryEntryLk> lkList = Lists.newArrayList();
                if(materialLkList!=null && materialLkList.size()>0) {
                	for(ZrkOrderProductMaterialLk materialLk:materialLkList) {
                		ZrkOrderDeliveryEntryLk lk = new ZrkOrderDeliveryEntryLk();
                		ZrkOrderProduct order = orderProductService.getById(materialLk.getProductId());
                		BasMaterial material = materialService.getById(materialLk.getMaterialId());
                		lk.setDeliveryEntryId(entry.getId());
                        lk.setMaterialId(materialLk.getMaterialId());
                        lk.setOrderId(materialLk.getOrderId());
                        if(order!=null) {
                        	lk.setUnitPrice(order.getUnitPrice());
                        }
                        if(material!=null) {
                        	lk.setQty(material.getQuantity());
                        }
                        lkList.add(lk);
                	}
                	lkService.saveBatch(lkList);
                }
            }
        }
        if(salesDelivery.getSalesType().equals(0)){
            //保存应收数据
            finaReceiptService.createReceivables1(salesDelivery, list);
        }

    }

    @Override
    public List<FreightOrIoBillVo> getDeliveryCode(){
    	List<String> billNoList = finaFreightMapper.getBillNo("1");
        return this.baseMapper.getDeliveryCode(billNoList);
    }

 @Override
    public  SalesDelivery getDeliveryByCode(String code){
        return this.baseMapper.getDeliveryByCode(code);
    }


}
